package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 需要被页面微服务调用
 */
@RestController
//api是为了让swagger发现 api是前台 admin是后台
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 获取商品详情页面所需要的数据详情 汇总
     * 优化 串行和并行 异步编排
     * 1和3并行 1和 2,4,5串行  2,4,5之间并行
     */
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId) {
        HashMap result = new HashMap<>();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1:根据skuId查询库存表sku_info  通过主键 同时查次库存图片集合
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //2: 一二三级分类 ID名称
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            //页面取的是categoryView
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //3: 单独查询价格 实时价格 避免查的缓存造成亏损
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //4: 查询销售属性及选中
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getSpuId(), skuInfo.getId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //5: 查询组合对应的SKUID 为了销售属性选择 跳转 刷新功能 返回JSON串
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //6: 某用户获取了页面详情 需要为此商品加1分 HotScore
        CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId,1);//将1分封装到常量
        }, threadPoolExecutor);

        //多任务组合 无需等待加分操作
        CompletableFuture.allOf(priceCompletableFuture, categoryViewCompletableFuture,
                spuSaleAttrListCompletableFuture, valuesSkuJsonCompletableFuture).join();
        return result;
    }
}
